package com.lesso.lpq.sunflower.infra.repository.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.lpq.sunflower.domain.repository.EmployeeDeviceRepository;
import com.lesso.lpq.sunflower.infra.assembler.EmployeeDeviceAssembler;
import com.lesso.lpq.sunflower.infra.mapper.EmployeeDeviceMapper;
import com.lesso.lpq.sunflower.model.EmployeeDevice;
import com.lesso.lpq.sunflower.pojo.dto.EmployeeDeviceDTO;
import com.lesso.lpq.sunflower.pojo.dto.EmployeeDeviceQueryDTO;
import com.lesso.lpq.sunflower.pojo.vo.EmployeeDeviceVO;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @ClassName : EmployeeDeviceRepositoryImpl
 * @Description :
 * @Author : 罗培棋
 * @Date: 2025-07-05 16:02
 */
@Repository
public class EmployeeDeviceRepositoryImpl implements EmployeeDeviceRepository {
    private final EmployeeDeviceMapper employeeDeviceMapper;

    public EmployeeDeviceRepositoryImpl(EmployeeDeviceMapper employeeDeviceMapper) {
        this.employeeDeviceMapper = employeeDeviceMapper;
    }

    @Override
    public Long save(EmployeeDevice employeeDevice) {
        if (employeeDevice.getId() == null) {
            employeeDeviceMapper.insert(employeeDevice);
        } else {
            employeeDeviceMapper.updateById(employeeDevice);
        }
        return employeeDevice.getId();
    }

    @Override
    public Page<EmployeeDeviceVO> getPageList(EmployeeDeviceQueryDTO dto) {
        Page<EmployeeDeviceVO> page = new Page<>(dto.getCurrent(), dto.getSize());
        EntityWrapper<EmployeeDevice> wrapper = new EntityWrapper<>();
        wrapper.eq("del_status", 0);

        List<EmployeeDevice> employeeDevices = employeeDeviceMapper.selectPage(page, wrapper);
        List<EmployeeDeviceVO> employeeDeviceVOs = EmployeeDeviceAssembler.INSTANCE.entityListToVOList(employeeDevices);
        page.setRecords(employeeDeviceVOs);
        return page;
    }

    @Override
    public Page<EmployeeDeviceVO> getCustomPageList(EmployeeDeviceQueryDTO dto) {
        Page<EmployeeDeviceVO> page = new Page<>(dto.getCurrent(), dto.getSize());
        RowBounds rowBounds = new RowBounds((dto.getCurrent() - 1) * dto.getSize(), dto.getSize());
        List<EmployeeDeviceVO> employeeDeviceVOs = employeeDeviceMapper.selectPageList(rowBounds, dto);
        page.setRecords(employeeDeviceVOs);
        return page;
    }

    @Override
    public void removeById(Long id) {
        employeeDeviceMapper.deleteById(id);
    }

    @Override
    public void updateById(EmployeeDeviceDTO dto) {
        EmployeeDevice employeeDevice = EmployeeDeviceAssembler.INSTANCE.dtoToEntity(dto);
        employeeDeviceMapper.updateById(employeeDevice);
    }

    @Override
    public EmployeeDeviceVO getByEmployeeId(String employeeId) {
        EntityWrapper<EmployeeDevice> wrapper = new EntityWrapper<>();
        wrapper.eq("employee_id",employeeId);
        wrapper.eq("del_status",0);
        EmployeeDevice employeeDevice = employeeDeviceMapper.selectById(wrapper);
        if (employeeDevice ==null){
            return null;
        }
        return EmployeeDeviceAssembler.INSTANCE.entityToVO(employeeDevice);
    }
}
